package com.uwosh.cs342.g6.mapapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class FavoriteList extends ListActivity {

	// All static variables
	final String URL = "http://softeng.cs.uwosh.edu/students/cs342g6/getFavorites.php?id="
			+ Login.currentUserId; // XML node keys
	final String KEY_ITEM = "favorite"; // parent node
	final String KEY_ID = "id";
	final String KEY_LAT = "lat";
	final String KEY_LON = "lon";
	final String KEY_NAME = "name";

	ArrayList<HashMap<String, String>> favoriteItems;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		favoriteItems = new ArrayList<HashMap<String, String>>();

		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL);
		Document doc = parser.getDomElement(xml);

		NodeList nl = doc.getElementsByTagName(KEY_ITEM);
		for (int i = 0; i < nl.getLength(); i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			Element e = (Element) nl.item(i);
			map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
			
			map.put(KEY_LAT, parser.getValue(e, KEY_LAT) + " " + parser.getValue(e, KEY_LON));
			map.put(KEY_LON, parser.getValue(e, KEY_LON));
			map.put(KEY_ID, parser.getValue(e, KEY_ID));
			favoriteItems.add(map);

		}
		ListAdapter adapter = new SimpleAdapter(this, favoriteItems,
				R.layout.activity_favorite_list, new String[] { KEY_NAME, KEY_LAT,
				KEY_LON, KEY_ID }, new int[] { R.id.name,
						R.id.desciption });

		setListAdapter(adapter);

		// selecting single ListView item
		ListView lv = getListView();

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				String[] temp = new String[2];
				temp = favoriteItems.get(position).get(KEY_LAT).split(" ");
				double lat = Double.parseDouble(temp[0]);
				
				double lon = Double.parseDouble(temp[1]);
				
				Intent in = new Intent(getApplicationContext(), MainActivity.class);
				in.putExtra("lat", lat+"");
				in.putExtra("lon", lon+"");
				finish();
				startActivity(in);


			}
		});
				
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {
						
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {

				

				return false;
			}
		});
	}

	private void onLongListItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		// getting values from selected ListItem
		String name = ((TextView) view.findViewById(R.id.name)).getText()
				.toString();
		String description = ((TextView) view.findViewById(R.id.desciption))
				.getText().toString();

		// call alert box for delete confirmation

	}



	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.favorite_list, menu);
		return true;
	}

}
